﻿using System.Collections.Generic;
using System.Data;
using POS.BusinessEntities;

namespace POS.DataObjects
{
    public class DOEntidad: DOBase  
    {
        #region Declarations
        
        private readonly string getListSP;
        private readonly string deleteSP;
        private readonly string saveSP;

        #endregion

        #region Constructors

        public DOEntidad(TipoEntidad entidad)
        {
            getListSP = entidad + "GetList";
            deleteSP = entidad + "Delete";
            saveSP = entidad + "Save";
        }

        #endregion

        #region Methods: Public

        public virtual List<BEEntidad> GetList(int empresaId, string filtro)
        {
            var result = new List<BEEntidad>();

            var command = Db.GetStoredProcCommand(getListSP);
            Db.AddInParameter(command, "@empresaId", DbType.Int32, empresaId);  
            Db.AddInParameter(command, "@filtro", DbType.String, filtro);
            var data = Db.ExecuteDataSet(command);

            if (data.Tables.Count == 1)
            {
                foreach (DataRow row in data.Tables[0].Rows)
                    result.Add(CreateEntityFromDataRow<BEEntidad>(row));
            }
            return result;
        }

        public virtual void Delete(int id, string user)
        {
            var command = Db.GetStoredProcCommand(deleteSP);
            Db.AddInParameter(command, "@id", DbType.Int32, id);
            Db.AddInParameter(command, "@user", DbType.String, user);
            Db.ExecuteNonQuery(command);
        }
        
        public virtual void Save(BEEntidad entidad, int empresaId, string user)
        {
            var command = Db.GetStoredProcCommand(saveSP);
            Db.AddInParameter(command, "@id", DbType.Int32, entidad.Id);
            Db.AddInParameter(command, "@name", DbType.String, entidad.Nombre);
            Db.AddInParameter(command, "@companyId", DbType.Int32, empresaId);
            Db.AddInParameter(command, "@user", DbType.String, user);
            Db.ExecuteNonQuery(command);
        }

        #endregion
    }
}
